<?php
/*==========================================================================*\
| AGRIVIET - CONTENT MANAGERMENT SYSTEM						
\*==========================================================================*/


	$pathdb = "uploads/portfolio"; // Duong dan ghi vao database		
	$imagepath="../uploads/portfolio/"; // Duong dan de upload Image
	$upload_allow_extension = "jpg,png,gif"; // Cac phan mo rong duoc cho phep

if (isset($_POST['butSave'])) {
	$published = $_POST['published'];
	$url_address = $_POST['url_address'];
	$date_add = mktime();
	$title = trim($_POST['title']);
	$alias = vietdecode($title);
	$intro = trim($_POST['intro']);
	
	$err="";	
	
	if ($err==''){
		/* Cap nhat hoac them moi */
		if (!empty($_POST['id'])) {
			$oldid = $_POST['id'];
			$sql = "update portfolio set title='".$title."',alias='".$alias."',intro='".$intro."',published='".$published."', ";
			$sql.= "date_add='".$date_add."', url_address='".$url_address."'";
			$sql.= " where id='".$oldid."'";
		}else {
			// Insert data
			$sql = "insert into portfolio (title,alias,intro,published,url_address,date_add)";
			$sql.= "values ('".$title."','".$alias."','".$intro."','".$published."','".$url_address."','".$date_add."')";
		}
		/* EOF - Cap nhat hoac them moi */
		if(mysql_query($sql)){ // Neu thuc hien query thanh cong
		
			if(empty($_POST['id'])) $oldid = mysql_insert_id();
			
			
			
		//Upload image here
		$filename="";
		if(is_uploaded_file($_FILES['img']['tmp_name']))
		{
			//Check file extension
			if (check_upload_extension($_FILES['img']['name'],$upload_allow_extension) == 1){
			$filename = generate_name('portfolio',$oldid,$_FILES['img']['name']);
			move_uploaded_file($_FILES['img']['tmp_name'],$imagepath . $filename);
			//resize_image($imagepath.$filename,$imagepath.'small'.$filename,150,200,100);
			//resize_image($imagepath.$filename,$imagepath.$filename,400,400,100);

			$_POST["img"] = $filename;
					
			$sqlUpdateField = " image ='$pathdb/".$filename."', imagelarge ='$pathdb/".$filename."' ";
			
				if($sqlUpdateField!='')
				{
					$sqlUpdate = "update portfolio set $sqlUpdateField where id='".$oldid."'";
					mysql_query($sqlUpdate);
				}
			}
		} else {
			$_POST["img"] = "";
		}
		// Eof Upload image
		} // Neu thuc hien Query thanh cong
		else {
			$err =  "Không th&#7875; c&#7853;p nh&#7853;t";
		}
		
	} // if ($err=='')
	
	if ($err=='') echo '<script>window.location="index.php?act=portfolio&code=1"</script>';
  	else echo "<p align=center class='red'>".$err."</p>";

} 
			
	$sql=mysql_query("SELECT * FROM languages order by ordering ASC");
	while($r=mysql_fetch_object($sql)){
	$bdtinyMCE .="body[".$r->languages_id."],";
	} ?>
<? action('Portfolio');
	if (isset($_GET['id'])){
	$result=mysql_query("SELECT * FROM portfolio WHERE id=$id");
	$row=mysql_fetch_object($result);
	$title=$row->title;
	$intro=$row->intro;
	$image=$row->image;
	$imagelarge=$row->imagelarge;
	$published=$row->published;
	$url_address=$row->url_address;
	$parent=$catid;
	}
	?>
  <table border="0" cellpadding="3" cellspacing="3" bordercolor="#CCCCCC">
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="hidden" name="act" value="portfolio_m">
<input type="hidden" name="id" value="<? echo $_REQUEST['id']; ?>">
<input type="hidden" name="page" value="<? echo $_REQUEST['page']; ?>">
	<tr>
      <td width="20%" align="right" valign="middle">Portfolio name:</td>
      <td align="right" valign="middle">

	    <img src="../<?=$r->image;?>" />	    </td>
      <td width="76%">  <input name="title" type="text" value="<?=$title;?>" size="50"></td>
    </tr>

	<tr>
      <td align="right" valign="top">Description </td>
      <td align="right" valign="top" >
	
	  <img src="../<?=$r->image;?>" align="top" /></td>
      <td valign="top" ><textarea name="intro" rows="4" id="intro"><?=$intro;?></textarea></td>
	</tr>
	 
	<tr>
	  <td align="right">Url Adress </td>
	  <td >&nbsp;</td>
	  <td ><input name="url_address" type="text" value="<?=$url_address;?>" size="50" /></td>
    </tr>
	<tr>
      <td align="right" valign="bottom">Image:</td>
      <td >	  </td>
      <td ><? if($image!='') {?>
	  <a href="../<?=$imagelarge;?>" target="_blank">
	  <img src="../<?=$image;?>" border="0" /></a><br /><? } ?>
	  <input name="img" type="file" id="img"></td>
	</tr>
	<tr>
	  <td align="right">Published: </td>	  
	  <td >&nbsp;</td>
      <td ><input name="published" type="checkbox" id="published" value="1" <? if ($published>0) echo 'checked' ?> /></td>
	</tr>
	<? 
	$sql=mysql_query("SELECT * FROM languages order by ordering ASC");
	while($r=mysql_fetch_object($sql)){
	?>
    <? } ?>
    <tr>
      <td>&nbsp;</td>
      <td></td>
      <td><label>
        <input name="butSave" type="submit" id="butSave" value="Update" class="button">
      </label></td>
    </tr></form>
</table>

